#!/usr/bin/env python
#-*- encoding:UTF-8 -*-

# $Id$

import wx
import connDB

import  string

#----------------------------------------------------------------------

ALPHA_ONLY = 1
DIGIT_ONLY = 2

class MyValidator(wx.PyValidator):
    
    def __init__(self, flag=None, pyVar=None):
        
        wx.PyValidator.__init__(self)
        
        self.flag = flag
        
        self.Bind(wx.EVT_CHAR, self.OnChar)

    def Clone(self):
        
        return MyValidator(self.flag)

    def Validate(self, win):
        
        tc = self.GetWindow()
        val = tc.GetValue()
        
        if self.flag == ALPHA_ONLY:
            for x in val:
                if x not in string.letters:
                    return False

        elif self.flag == DIGIT_ONLY:
            for x in val:
                if x not in string.digits:
                    return False

        return True


    def OnChar(self, event):
        key = event.GetKeyCode()

        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
            event.Skip()
            return

        if self.flag == ALPHA_ONLY and chr(key) in string.letters:
            event.Skip()
            return

        if self.flag == DIGIT_ONLY and chr(key) in string.digits:
            event.Skip()
            return

        if not wx.Validator_IsSilent():
            wx.Bell()

        # Returning without calling even.Skip eats the event before it
        # gets to the text control
        return
    
    
class NewDataset(wx.Dialog):
    
    """
    Create a New Dataset in SDS
    """
    
    def __init__(
            self, parent, classlst, title="New Dataset", size=(400,300), pos=wx.DefaultPosition, 
            style=wx.DEFAULT_DIALOG_STYLE
            ):
        
        [wxID_Dataset,
         wxID_Class,
         wxID_Purpose,
         wxID_Version,
         wxID_OK,
         wxID_CANCEL,
         wxID_Note
        ] = [wx.NewId() for _init_ctrls in range(7)]
        
        wx.Dialog.__init__(self, parent, id=wx.ID_ANY, title=title, size=size, pos=pos, style=style)
        
        panel = wx.Panel(self, id=wx.ID_ANY)
        
        
        self.lb1 = wx.StaticText(panel, wx.ID_ANY, "Dataset:")
        self.te1 = wx.TextCtrl(panel, wxID_Dataset, "", size=(130, -1), validator = MyValidator(ALPHA_ONLY))
        #self.te1.SetMaxLength(6)
        self.te1.SetMaxLength(2)
        
        self.lb2 = wx.StaticText(panel, wx.ID_ANY, "Class:")
        self.te2 = wx.ComboBox(panel, wxID_Class, size=(160, -1), choices=classlst)
        
        #to-do: Purpose list
        purposelst=["Tabulation"]
        self.lb3 = wx.StaticText(panel, wx.ID_ANY, "Purpose:")
        self.te3 = wx.ComboBox(panel, wxID_Purpose, "Tabulation", size=(130, -1), choices=purposelst)
        
        #to-do: Version list
        versionlst=["3.1.1"]
        self.lb4 = wx.StaticText(panel, wx.ID_ANY, "Version:")
        self.te4 = wx.ComboBox(panel, wxID_Version, "3.1.1", size=(130, -1), choices=versionlst)
        
        
        #FlexGridSizer
        fgs = wx.FlexGridSizer(4,2,5,5)
        fgs.AddMany([(self.lb1, 0, wx.ALIGN_RIGHT | wx.LEFT | wx.TOP, 5), (self.te1, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5),
                     (self.lb2, 0, wx.ALIGN_RIGHT | wx.LEFT | wx.TOP, 5), (self.te2, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5),
                     (self.lb3, 0, wx.ALIGN_RIGHT | wx.LEFT | wx.TOP, 5), (self.te3, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5),
                     (self.lb4, 0, wx.ALIGN_RIGHT | wx.LEFT | wx.TOP, 5), (self.te4, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5),
                    ])
        fgs.AddGrowableRow(8, 1)
        fgs.AddGrowableCol(1, 1)
        
         # StaticBoxSizer
        staticbox = wx.StaticBox(panel, id=wx.ID_ANY, label="New Dataset:")
        staticboxsizer = wx.StaticBoxSizer(staticbox, wx.VERTICAL)
        staticboxsizer.Add(fgs, 6, wx.EXPAND | wx.ALL, 5)
        

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        okButton = wx.Button(panel, id=wx.ID_OK, label="Ok")
        CancelButton = wx.Button(panel, id=wx.ID_CANCEL, label="Cancel")
        hbox.Add(okButton, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
        hbox.Add(CancelButton, 0, wx.ALIGN_RIGHT | wx.ALL, 5)



        
        note = wx.StaticText(panel,id=wxID_Note,label="Dataset Name Should Contain Only Two Characters!")
        notebox = wx.StaticBox(panel, id=wx.ID_ANY, label="Note:")
        noteboxsizer = wx.StaticBoxSizer(notebox, wx.VERTICAL)
        noteboxsizer.Add(note, 0, wx.EXPAND | wx.ALL, 5)
        
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(staticboxsizer, 0, wx.EXPAND | wx.ALL, 5)
        vbox.Add(hbox, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
        vbox.Add(noteboxsizer, 0, wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, 5)

        panel.SetSizer(vbox)
        
        pbox = wx.BoxSizer(wx.VERTICAL)
        pbox.Add(panel, 1, wx.EXPAND | wx.ALL)
        
        self.SetSizer(pbox)
        
        
    def GetValue(self):
        """
        return the dataset name, class, purpose, version
        """
        return (self.te1.Value, self.te2.Value, self.te3.Value, self.te4.Value)
    
